
<!doctype html>
<html lang="zh" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
      
      
      
        <link rel="prev" href="../0-overview/">
      
      
        <link rel="next" href="../2-step0/">
      
      <link rel="icon" href="../../assets/images/favicon.png">
      <meta name="generator" content="mkdocs-1.5.3, mkdocs-material-9.1.15">
    
    
      
        <title>实验原理 - 计算机组成原理实验（2024春季） | 哈工大（深圳）</title>
      
    
    
      <link rel="stylesheet" href="../../assets/stylesheets/main.26e3688c.min.css">
      
        
        <link rel="stylesheet" href="../../assets/stylesheets/palette.ecc896b0.min.css">
      
      

    
    
    
      
        
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
        <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
      
    
    
    <script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce((e,_)=>(e<<5)-e+_.charCodeAt(0),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
    
      

    
    
    
  </head>
  
  
    
    
      
    
    
    
    
    <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="orange" data-md-color-accent="orange">
  
    
    
      <script>var palette=__md_get("__palette");if(palette&&"object"==typeof palette.color)for(var key of Object.keys(palette.color))document.body.setAttribute("data-md-color-"+key,palette.color[key])</script>
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#_1" class="md-skip">
          跳转至
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
    
      

<header class="md-header" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="页眉">
    <a href="../.." title="计算机组成原理实验（2024春季） | 哈工大（深圳）" class="md-header__button md-logo" aria-label="计算机组成原理实验（2024春季） | 哈工大（深圳）" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M384 96v224H64V96h320zM64 32C28.7 32 0 60.7 0 96v224c0 35.3 28.7 64 64 64h117.3l-10.7 32H96c-17.7 0-32 14.3-32 32s14.3 32 32 32h256c17.7 0 32-14.3 32-32s-14.3-32-32-32h-74.7l-10.7-32H384c35.3 0 64-28.7 64-64V96c0-35.3-28.7-64-64-64H64zm464 0c-26.5 0-48 21.5-48 48v352c0 26.5 21.5 48 48 48h64c26.5 0 48-21.5 48-48V80c0-26.5-21.5-48-48-48h-64zm16 64h32c8.8 0 16 7.2 16 16s-7.2 16-16 16h-32c-8.8 0-16-7.2-16-16s7.2-16 16-16zm-16 80c0-8.8 7.2-16 16-16h32c8.8 0 16 7.2 16 16s-7.2 16-16 16h-32c-8.8 0-16-7.2-16-16zm32 160a32 32 0 1 1 0 64 32 32 0 1 1 0-64z"/></svg>

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            计算机组成原理实验（2024春季） | 哈工大（深圳）
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              实验原理
            
          </span>
        </div>
      </div>
    </div>
    
      
        <form class="md-header__option" data-md-component="palette">
          
            
            
            
            <input class="md-option" data-md-color-media="" data-md-color-scheme="default" data-md-color-primary="orange" data-md-color-accent="orange"  aria-label="切换到夜晚模式"  type="radio" name="__palette" id="__palette_1">
            
              <label class="md-header__button md-icon" title="切换到夜晚模式" for="__palette_2" hidden>
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 7a5 5 0 0 1 5 5 5 5 0 0 1-5 5 5 5 0 0 1-5-5 5 5 0 0 1 5-5m0 2a3 3 0 0 0-3 3 3 3 0 0 0 3 3 3 3 0 0 0 3-3 3 3 0 0 0-3-3m0-7 2.39 3.42C13.65 5.15 12.84 5 12 5c-.84 0-1.65.15-2.39.42L12 2M3.34 7l4.16-.35A7.2 7.2 0 0 0 5.94 8.5c-.44.74-.69 1.5-.83 2.29L3.34 7m.02 10 1.76-3.77a7.131 7.131 0 0 0 2.38 4.14L3.36 17M20.65 7l-1.77 3.79a7.023 7.023 0 0 0-2.38-4.15l4.15.36m-.01 10-4.14.36c.59-.51 1.12-1.14 1.54-1.86.42-.73.69-1.5.83-2.29L20.64 17M12 22l-2.41-3.44c.74.27 1.55.44 2.41.44.82 0 1.63-.17 2.37-.44L12 22Z"/></svg>
              </label>
            
          
            
            
            
            <input class="md-option" data-md-color-media="" data-md-color-scheme="slate" data-md-color-primary="orange" data-md-color-accent="orange"  aria-label="切换到白天模式"  type="radio" name="__palette" id="__palette_2">
            
              <label class="md-header__button md-icon" title="切换到白天模式" for="__palette_1" hidden>
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m17.75 4.09-2.53 1.94.91 3.06-2.63-1.81-2.63 1.81.91-3.06-2.53-1.94L12.44 4l1.06-3 1.06 3 3.19.09m3.5 6.91-1.64 1.25.59 1.98-1.7-1.17-1.7 1.17.59-1.98L15.75 11l2.06-.05L18.5 9l.69 1.95 2.06.05m-2.28 4.95c.83-.08 1.72 1.1 1.19 1.85-.32.45-.66.87-1.08 1.27C15.17 23 8.84 23 4.94 19.07c-3.91-3.9-3.91-10.24 0-14.14.4-.4.82-.76 1.27-1.08.75-.53 1.93.36 1.85 1.19-.27 2.86.69 5.83 2.89 8.02a9.96 9.96 0 0 0 8.02 2.89m-1.64 2.02a12.08 12.08 0 0 1-7.8-3.47c-2.17-2.19-3.33-5-3.49-7.82-2.81 3.14-2.7 7.96.31 10.98 3.02 3.01 7.84 3.12 10.98.31Z"/></svg>
              </label>
            
          
        </form>
      
    
    
    
      <label class="md-header__button md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
      </label>
      <div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" aria-label="搜索" placeholder="搜索" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required>
      <label class="md-search__icon md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
      </label>
      <nav class="md-search__options" aria-label="查找">
        
        <button type="reset" class="md-search__icon md-icon" title="清空当前内容" aria-label="清空当前内容" tabindex="-1">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41Z"/></svg>
        </button>
      </nav>
      
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="search-result">
          <div class="md-search-result__meta">
            正在初始化搜索引擎
          </div>
          <ol class="md-search-result__list" role="presentation"></ol>
        </div>
      </div>
    </div>
  </div>
</div>
    
    
      <div class="md-header__source">
        <a href="https://gitee.com/hitsz-cslab/organ" title="前往仓库" class="md-source" data-md-component="source">
  <div class="md-source__icon md-icon">
    
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M439.55 236.05 244 40.45a28.87 28.87 0 0 0-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 0 1-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 0 0 0 40.81l195.61 195.6a28.86 28.86 0 0 0 40.8 0l194.69-194.69a28.86 28.86 0 0 0 0-40.81z"/></svg>
  </div>
  <div class="md-source__repository">
    HITSZ-COMP2008
  </div>
</a>
      </div>
    
  </nav>
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
          
            
<nav class="md-tabs" aria-label="标签" data-md-component="tabs">
  <div class="md-grid">
    <ul class="md-tabs__list">
      
        
  
  


  
  
  
    <li class="md-tabs__item">
      <a href="../.." class="md-tabs__link">
        实验须知
      </a>
    </li>
  

      
        
  
  
    
  


  
  
  
    <li class="md-tabs__item">
      <a href="../0-overview/" class="md-tabs__link md-tabs__link--active">
        实验1 从C语言到机器码
      </a>
    </li>
  

      
    </ul>
  </div>
</nav>
          
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

  


<nav class="md-nav md-nav--primary md-nav--lifted" aria-label="导航栏" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href="../.." title="计算机组成原理实验（2024春季） | 哈工大（深圳）" class="md-nav__button md-logo" aria-label="计算机组成原理实验（2024春季） | 哈工大（深圳）" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M384 96v224H64V96h320zM64 32C28.7 32 0 60.7 0 96v224c0 35.3 28.7 64 64 64h117.3l-10.7 32H96c-17.7 0-32 14.3-32 32s14.3 32 32 32h256c17.7 0 32-14.3 32-32s-14.3-32-32-32h-74.7l-10.7-32H384c35.3 0 64-28.7 64-64V96c0-35.3-28.7-64-64-64H64zm464 0c-26.5 0-48 21.5-48 48v352c0 26.5 21.5 48 48 48h64c26.5 0 48-21.5 48-48V80c0-26.5-21.5-48-48-48h-64zm16 64h32c8.8 0 16 7.2 16 16s-7.2 16-16 16h-32c-8.8 0-16-7.2-16-16s7.2-16 16-16zm-16 80c0-8.8 7.2-16 16-16h32c8.8 0 16 7.2 16 16s-7.2 16-16 16h-32c-8.8 0-16-7.2-16-16zm32 160a32 32 0 1 1 0 64 32 32 0 1 1 0-64z"/></svg>

    </a>
    计算机组成原理实验（2024春季） | 哈工大（深圳）
  </label>
  
    <div class="md-nav__source">
      <a href="https://gitee.com/hitsz-cslab/organ" title="前往仓库" class="md-source" data-md-component="source">
  <div class="md-source__icon md-icon">
    
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M439.55 236.05 244 40.45a28.87 28.87 0 0 0-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 0 1-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 0 0 0 40.81l195.61 195.6a28.86 28.86 0 0 0 40.8 0l194.69-194.69a28.86 28.86 0 0 0 0-40.81z"/></svg>
  </div>
  <div class="md-source__repository">
    HITSZ-COMP2008
  </div>
</a>
    </div>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
        
      
      <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_1" >
      
      
      
        <label class="md-nav__link" for="__nav_1" id="__nav_1_label" tabindex="0">
          实验须知
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_1_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_1">
          <span class="md-nav__icon md-icon"></span>
          实验须知
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../.." class="md-nav__link">
        实验介绍
      </a>
    </li>
  

            
          
            
              
  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
        
      
      <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_1_2" >
      
      
      
        <label class="md-nav__link" for="__nav_1_2" id="__nav_1_2_label" tabindex="0">
          Verilog语法回顾
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="2" aria-labelledby="__nav_1_2_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_1_2">
          <span class="md-nav__icon md-icon"></span>
          Verilog语法回顾
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part0/" class="md-nav__link">
        初学Verilog的几点提醒
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part1/" class="md-nav__link">
        模块的基本结构
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part2/" class="md-nav__link">
        标识符和数据类型
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part3/" class="md-nav__link">
        运算符及表达式
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part4/" class="md-nav__link">
        逻辑门的描述
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part5/" class="md-nav__link">
        赋值语句和块语句
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part6/" class="md-nav__link">
        条件语句和循环语句
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/defparam/" class="md-nav__link">
        宏定义和参数
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part7/" class="md-nav__link">
        模块的实例化
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/module_test/" class="md-nav__link">
        模块的测试
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part8/" class="md-nav__link">
        4位全加器设计实例
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/operation/" class="md-nav__link">
        实验提交说明
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
    
  
  
    
    <li class="md-nav__item md-nav__item--active md-nav__item--nested">
      
      
      
      
      <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_2" checked>
      
      
      
        <label class="md-nav__link" for="__nav_2" id="__nav_2_label" tabindex="0">
          实验1 从C语言到机器码
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_2_label" aria-expanded="true">
        <label class="md-nav__title" for="__nav_2">
          <span class="md-nav__icon md-icon"></span>
          实验1 从C语言到机器码
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../0-overview/" class="md-nav__link">
        实验概述
      </a>
    </li>
  

            
          
            
              
  
  
    
  
  
    <li class="md-nav__item md-nav__item--active">
      
      <input class="md-nav__toggle md-toggle" type="checkbox" id="__toc">
      
      
        
      
      
        <label class="md-nav__link md-nav__link--active" for="__toc">
          实验原理
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <a href="./" class="md-nav__link md-nav__link--active">
        实验原理
      </a>
      
        

<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      目录
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#1-c" class="md-nav__link">
    1. C程序与汇编、机器码的关系
  </a>
  
    <nav class="md-nav" aria-label="1. C程序与汇编、机器码的关系">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#11" class="md-nav__link">
    1.1 编译阶段
  </a>
  
    <nav class="md-nav" aria-label="1.1 编译阶段">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#111" class="md-nav__link">
    1.1.1 预编译
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#112" class="md-nav__link">
    1.1.2 编译
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#12" class="md-nav__link">
    1.2 汇编阶段
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#13" class="md-nav__link">
    1.3 链接阶段
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#14" class="md-nav__link">
    1.4 加载运行阶段
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#15" class="md-nav__link">
    1.5 反汇编
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#2-risc-v" class="md-nav__link">
    2. RISC-V汇编程序设计
  </a>
  
    <nav class="md-nav" aria-label="2. RISC-V汇编程序设计">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#21" class="md-nav__link">
    2.1 子程序
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#22" class="md-nav__link">
    2.2 输入与打印
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
      
    </li>
  

            
          
            
              
  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
        
      
      <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_2_3" >
      
      
      
        <label class="md-nav__link" for="__nav_2_3" id="__nav_2_3_label" tabindex="0">
          实验步骤
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="2" aria-labelledby="__nav_2_3_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_2_3">
          <span class="md-nav__icon md-icon"></span>
          实验步骤
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../2-step0/" class="md-nav__link">
        开始之前
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../2-step1/" class="md-nav__link">
        C语言到汇编
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../2-step2/" class="md-nav__link">
        编写汇编程序
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../3-extra/" class="md-nav__link">
        附加题
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../4-submit/" class="md-nav__link">
        验收与提交
      </a>
    </li>
  

            
          
            
              
  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
        
      
      <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_2_6" >
      
      
      
        <label class="md-nav__link" for="__nav_2_6" id="__nav_2_6_label" tabindex="0">
          附录
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="2" aria-labelledby="__nav_2_6_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_2_6">
          <span class="md-nav__icon md-icon"></span>
          附录
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../A-envir/" class="md-nav__link">
        A. 实验环境安装
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../B-lnxcmd/" class="md-nav__link">
        B. Linux常用命令
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../C-rv_ref/" class="md-nav__link">
        C. RISC-V参考资料
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              
              <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      目录
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#1-c" class="md-nav__link">
    1. C程序与汇编、机器码的关系
  </a>
  
    <nav class="md-nav" aria-label="1. C程序与汇编、机器码的关系">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#11" class="md-nav__link">
    1.1 编译阶段
  </a>
  
    <nav class="md-nav" aria-label="1.1 编译阶段">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#111" class="md-nav__link">
    1.1.1 预编译
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#112" class="md-nav__link">
    1.1.2 编译
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#12" class="md-nav__link">
    1.2 汇编阶段
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#13" class="md-nav__link">
    1.3 链接阶段
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#14" class="md-nav__link">
    1.4 加载运行阶段
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#15" class="md-nav__link">
    1.5 反汇编
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#2-risc-v" class="md-nav__link">
    2. RISC-V汇编程序设计
  </a>
  
    <nav class="md-nav" aria-label="2. RISC-V汇编程序设计">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#21" class="md-nav__link">
    2.1 子程序
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#22" class="md-nav__link">
    2.2 输入与打印
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
                  </div>
                </div>
              </div>
            
          
          
            <div class="md-content" data-md-component="content">
              <article class="md-content__inner md-typeset">
                
                  


<h1 id="_1">实验原理</h1>
<h2 id="1-c">1. C程序与汇编、机器码的关系</h2>
<p>&emsp;&emsp;编写好的C程序通常要经过 <strong>编译</strong> 和 <strong>汇编</strong> 过程才能变成机器码，而汇编得到的机器码还需经过 <strong>链接</strong> 和 <strong>加载运行</strong> 才能变成真正可运行的程序，如图2-1所示。</p>
<p><center><img src="../assets/2-1.png" width = 280></center>
<center>图2-1 C程序从编译到执行的过程</center></p>
<h3 id="11">1.1 编译阶段</h3>
<p>&emsp;&emsp;在编译阶段，编译器（Compiler）将C程序（<code>.c</code>）经过编译，生成汇编程序（<code>.s</code>或<code>.asm</code>）。进一步地，编译阶段分为预编译和编译两个步骤。</p>
<h4 id="111">1.1.1 预编译</h4>
<p>&emsp;&emsp;预编译即预处理，主要执行以下操作：</p>
<p>&emsp;&emsp;1）<strong>宏定义处理</strong>：</p>
<p>&emsp;&emsp;&emsp;&emsp;删除<code>#define</code>语句，并展开相应的宏定义。<br />
&emsp;&emsp;&emsp;&emsp;处理条件预编译指令，如<code>#if</code>、<code>#ifdef</code>、<code>#elif</code>、<code>#else</code>、<code>#endif</code>等。</p>
<p>&emsp;&emsp;2）<strong>预编译指令处理</strong>：</p>
<p>&emsp;&emsp;&emsp;&emsp;处理<code>#include</code>预编译指令，将被包含的文件插入到该预编译指令的位置。</p>
<p>&emsp;&emsp;3）<strong>删除注释</strong>：</p>
<p>&emsp;&emsp;&emsp;&emsp;删除所有注释，包括单行注释“<code>//</code>”和多行注释“<code>/* */</code>”。</p>
<p>&emsp;&emsp;4）<strong>添加行号和文件标识</strong>：</p>
<p>&emsp;&emsp;&emsp;&emsp;在源文件中添加行号和文件标识，以便编译时产生调试用的行号及编译错误警告行号。</p>
<p>&emsp;&emsp;5）<strong>保留编译器指令</strong>：</p>
<p>&emsp;&emsp;&emsp;&emsp;保留所有的<code>#pragma</code>编译器指令，后续编译过程需要使用它们。</p>
<h4 id="112">1.1.2 编译</h4>
<p>&emsp;&emsp;编译器对预处理后的文件进行<u>词法分析</u>、<u>语法分析</u>、<u>语义分析</u>、<u>中间代码生成及优化</u>、<u>目标代码生成</u>等，最终生成汇编程序。</p>
<h3 id="12">1.2 汇编阶段</h3>
<p>&emsp;&emsp;在汇编阶段，汇编器（Assembler）将汇编程序（<code>.s</code>或<code>.asm</code>）翻译生成<a href="https://wiki.osdev.org/ELF" target="_blank">ELF格式</a>（Executable and Linkable Format）的目标文件（<code>.o</code>）。目标文件由机器码组成。</p>
<p>&emsp;&emsp;汇编时，每一条汇编语句几乎都对应一条机器码指令。因此，汇编比编译简单许多 —— 汇编器只需要根据指令的编码格式，将汇编指令逐条翻译成机器码指令。</p>
<p>&emsp;&emsp;若源程序由多个源文件构成，则每个源文件都要先经过编译和汇编生成相应的目标文件。</p>
<p>&emsp;&emsp;目标文件内的程序是以机器码的形式存放的，且<u>每一个目标文件都是最终程序的一部分</u>。只有将它们链接起来形成完整的程序，才能被CPU执行。</p>
<div class="admonition info">
<p class="admonition-title">小拓展 <img alt="📖" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4d6.svg" title=":book:" /></p>
<p>&emsp;&emsp;一个ELF文件通常包含以下内容：</p>
<p>&emsp;&emsp;<img alt="💠" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4a0.svg" title=":diamond_shape_with_a_dot_inside:" /> <strong>ELF Header</strong>：一个<code>16B</code>的字节序列，描述系统字（<code>Word</code>）的大小、字节顺序等；</p>
<p>&emsp;&emsp;<img alt="💠" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4a0.svg" title=":diamond_shape_with_a_dot_inside:" /> <strong>.text</strong>：代码段（Text Segment），即程序的机器代码；</p>
<p>&emsp;&emsp;<img alt="💠" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4a0.svg" title=":diamond_shape_with_a_dot_inside:" /> <strong>.data</strong>：数据段（Data Segment），存放代码中具有非零初始值的全局变量；</p>
<p>&emsp;&emsp;<img alt="💠" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4a0.svg" title=":diamond_shape_with_a_dot_inside:" /> <strong>.rodata</strong>：只读数据段，存放代码中的常量，如字符串常量、全局常量等；</p>
<p>&emsp;&emsp;<img alt="💠" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4a0.svg" title=":diamond_shape_with_a_dot_inside:" /> <strong>.bss</strong>：存放未被初始化或初始值为0的全局变量和静态变量；</p>
<p>&emsp;&emsp;<img alt="💠" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4a0.svg" title=":diamond_shape_with_a_dot_inside:" /> <strong>.symtab</strong>：符号表，存放代码中定义和引用的符号信息，如函数、变量、常量等；</p>
<p>&emsp;&emsp;<img alt="💠" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4a0.svg" title=":diamond_shape_with_a_dot_inside:" /> <strong>.debug</strong>：存放调试信息，包含源代码、行号等。只有编译时添加“<code>-g</code>”选项才会生成；</p>
<p>&emsp;&emsp;<img alt="💠" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4a0.svg" title=":diamond_shape_with_a_dot_inside:" /> <strong>.strtab</strong>：字符串表，包含定义的字符串和节（section）的名字。</p>
</div>
<h3 id="13">1.3 链接阶段</h3>
<p>&emsp;&emsp;汇编生成的目标文件不能直接运行。为了得到能够被操作系统加载运行的可执行文件，必须在目标文件中包含固定格式的信息头，还必须将目标文件与系统提供的启动代码链接起来。这些工作都是由链接器完成的。</p>
<p>&emsp;&emsp;在链接阶段，链接器将ELF格式的目标文件（<code>.o</code>）链接起来，形成可执行文件。</p>
<p>&emsp;&emsp;链接一般需要完成以下操作：</p>
<p>&emsp;&emsp;1）<strong>获取重定向表</strong>：</p>
<p>&emsp;&emsp;&emsp;&emsp;从目标文件取出重定向表，获取需要重定向的符号、重定向的类型、重定向的地址等等。</p>
<p>&emsp;&emsp;2）<strong>重定向</strong>：</p>
<p>&emsp;&emsp;&emsp;&emsp;根据符号表和重定向表信息，将程序中引用的符号替换成绝对地址或偏移地址。</p>
<p>&emsp;&emsp;3）<strong>合并节</strong>：</p>
<p>&emsp;&emsp;&emsp;&emsp;合并各目标文件的代码段和数据段，形成可执行文件。</p>
<p>&emsp;&emsp;4）<strong>合并符号</strong>：</p>
<p>&emsp;&emsp;&emsp;&emsp;若多个目标文件定义了相同的符号，将这些符号合并。</p>
<h3 id="14">1.4 加载运行阶段</h3>
<p>&emsp;&emsp;在加载阶段，通常由操作系统加载可执行文件并运行程序。</p>
<p>&emsp;&emsp;加载时通常需要完成以下操作：</p>
<p>&emsp;&emsp;1）<strong>加载ELF头</strong>：</p>
<p>&emsp;&emsp;&emsp;&emsp;操作系统读取可执行文件，检查文件有效性，并从文件头获取各个节的数量和大小。</p>
<p>&emsp;&emsp;2）<strong>分配地址空间</strong>：</p>
<p>&emsp;&emsp;&emsp;&emsp;根据节的大小信息分配虚拟地址空间。地址空间内含代码段、数据段、堆栈段等。</p>
<p>&emsp;&emsp;3）<strong>装入目标程序</strong>：</p>
<p>&emsp;&emsp;&emsp;&emsp;将目标程序的代码段和数据段装入到所分配的虚拟地址空间。</p>
<p>&emsp;&emsp;4）<strong>设置入口地址</strong>：</p>
<p>&emsp;&emsp;&emsp;&emsp;将系统调用的返回地址设置为目标程序的入口地址。</p>
<p>&emsp;&emsp;&emsp;&emsp;若目标程序采用静态链接方式，则入口地址为ELF头中<code>e_entry</code>所指向的地址；</p>
<p>&emsp;&emsp;&emsp;&emsp;若目标程序采用动态链接方式，则入口地址是动态链接程序的入口地址。</p>
<p>&emsp;&emsp;5）<strong>设置参数和环境</strong>：</p>
<p>&emsp;&emsp;&emsp;&emsp;将必要的参数、环境变量等复制到栈上，供程序运行使用。</p>
<p>&emsp;&emsp;6）<strong>执行目标程序</strong>：</p>
<p>&emsp;&emsp;&emsp;&emsp;设置程序指针寄存器、堆栈指针寄存器等，开始执行目标程序。</p>
<h3 id="15">1.5 反汇编</h3>
<p>&emsp;&emsp;汇编是将汇编代码翻译成机器码，反汇编则是将机器码重新翻译成汇编代码，是汇编过程的反过程。</p>
<p>&emsp;&emsp;反汇编是用于调试和定位CPU问题的最常用的手段之一。有时候我们无法获取到可执行文件对应的高级语言程序代码，因为ELF文件无法当做普通的文本文件打开。此时，我们可以使用反汇编工具，将ELF文件中的机器码翻译成汇编指令。此外，反汇编工具还能打印ELF文件的其他信息，如数据段。</p>
<h2 id="2-risc-v">2. RISC-V汇编程序设计</h2>
<h3 id="21">2.1 子程序</h3>
<p>&emsp;&emsp;子程序是一段具有特定功能的、能被重复调用的代码。在程序设计中使用子程序，有利于增加代码可读性和可维护性。</p>
<p>&emsp;&emsp;RISC-V的子程序并不像X86那样需要使用专门的关键字来定义。从形式上看，RISC-V汇编的子程序可以看作是一段带有标签的、可重复执行的代码。</p>
<p>&emsp;&emsp;下列代码是一个在主程序中使用子程序的例子。</p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span>
<span class="normal">12</span>
<span class="normal">13</span>
<span class="normal">14</span>
<span class="normal">15</span>
<span class="normal">16</span>
<span class="normal">17</span>
<span class="normal">18</span>
<span class="normal">19</span>
<span class="normal">20</span>
<span class="normal">21</span>
<span class="normal">22</span>
<span class="normal">23</span>
<span class="normal">24</span>
<span class="normal">25</span>
<span class="normal">26</span>
<span class="normal">27</span>
<span class="normal">28</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="na">.data</span>
<span class="w">    </span><span class="na">......</span>

<span class="na">.macro</span><span class="w"> </span><span class="no">push</span><span class="w"> </span><span class="nv">%a</span>
<span class="w">    </span><span class="nf">addi</span><span class="w"> </span><span class="no">sp</span><span class="p">,</span><span class="w"> </span><span class="no">sp</span><span class="p">,</span><span class="w"> </span><span class="mi">-4</span>
<span class="w">    </span><span class="nf">sw</span><span class="w">   </span><span class="nv">%a</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">(</span><span class="no">sp</span><span class="p">)</span><span class="w"> </span>
<span class="no">.end_macro</span>

<span class="na">.macro</span><span class="w"> </span><span class="no">pop</span><span class="w"> </span><span class="nv">%a</span>
<span class="w">    </span><span class="nf">lw</span><span class="w">   </span><span class="nv">%a</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">(</span><span class="no">sp</span><span class="p">)</span><span class="w"> </span>
<span class="w">    </span><span class="no">addi</span><span class="w"> </span><span class="no">sp</span><span class="p">,</span><span class="w"> </span><span class="no">sp</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span>
<span class="na">.end_macro</span>

<span class="na">.text</span>
<span class="nl">MAIN:</span>
<span class="w">    </span><span class="na">......</span>
<span class="w">    </span><span class="nf">jal</span><span class="w">  </span><span class="no">ra</span><span class="p">,</span><span class="w"> </span><span class="no">SOME_FUNC</span><span class="w">       </span><span class="c1"># Call a sub-routine.</span>
<span class="w">    </span><span class="na">......</span><span class="w">                   </span><span class="c1"># Here is where the sub-routine returns.</span>
<span class="w">    </span><span class="nf">ori</span><span class="w">  </span><span class="no">a7</span><span class="p">,</span><span class="w"> </span><span class="no">zero</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="w">        </span><span class="c1"># Set system call number(10 for termination).</span>
<span class="w">    </span><span class="nf">ecall</span><span class="w">                    </span><span class="c1"># This program terminates here.</span>

<span class="nl">SOME_FUNC:</span>
<span class="w">    </span><span class="nf">push</span><span class="w"> </span><span class="no">t0</span>
<span class="w">    </span><span class="nf">push</span><span class="w"> </span><span class="no">t1</span>
<span class="w">    </span><span class="na">......</span><span class="w">                   </span><span class="c1"># Assume that t0 and t1 are modified here.</span>
<span class="w">    </span><span class="nf">pop</span><span class="w">  </span><span class="no">t1</span>
<span class="w">    </span><span class="nf">pop</span><span class="w">  </span><span class="no">t0</span>
<span class="w">    </span><span class="nf">jalr</span><span class="w"> </span><span class="no">zero</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">(</span><span class="no">ra</span><span class="p">)</span><span class="w">         </span><span class="c1"># The sub-routine returns.</span>
</code></pre></div></td></tr></table></div>
<ul>
<li>
<p>第4-7行：<code>push</code>宏定义将一个寄存器压入栈中。</p>
</li>
<li>
<p>第9-12行：<code>pop</code>宏定义将栈顶的数据弹出到指定寄存器。</p>
</li>
<li>
<p>第17行：主程序通过<code>jal  ra, FUNC</code>指令进入子程序，并将返回地址保存至<code>ra</code>寄存器。</p>
</li>
<li>
<p>第19-20行：停机指令。</p>
</li>
<li>
<p>第23-24行：<strong>保护现场</strong> —— 除去用于传递返回值的寄存器之外，子程序在修改任意寄存器之前，都需要先将其压入栈中，防止子程序返回后造成主程序执行出错。</p>
</li>
<li>
<p>第26-27行：<strong>恢复现场</strong> —— 子程序返回之前，从栈中恢复那些被子程序修改过的寄存器。</p>
</li>
<li>
<p>第28行：子程序通过<code>jalr zero, 0(ra)</code>指令返回到主程序第18行继续执行。</p>
</li>
</ul>
<div class="admonition tip">
<p class="admonition-title">压栈和弹栈的顺序 <img alt="📚" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4da.svg" title=":books:" /></p>
<p>&emsp;&emsp;因为栈内的数据是后进先出的，所以保护现场时压栈的顺序应和恢复现场时弹栈的顺序相反。</p>
</div>
<h3 id="22">2.2 输入与打印</h3>
<p>&emsp;&emsp;RARS通过系统调用语句提供了输入输出功能。用户需要按照约定，设置参数寄存器，然后执行<code>ecall</code>指令即可。</p>
<p>&emsp;&emsp;在RARS中，<code>a7</code>寄存器用于传递系统调用的编号，具体参数则通过<code>a0</code>、<code>a1</code>等寄存器设置。详见表2-1。</p>
<p><center>表2-1 RARS常用系统调用</center>
<center></p>
<table>
<thead>
<tr>
<th align="center">用途</th>
<th align="center"><code>a7</code>的值</th>
<th align="left">参数设置</th>
<th align="left">返回值</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">打印整数</td>
<td align="center">1</td>
<td align="left"><code>a0</code>存放待打印数据</td>
<td align="left">无</td>
</tr>
<tr>
<td align="center">打印字符串</td>
<td align="center">4</td>
<td align="left"><code>a0</code>存放待打印字符串（字符串需以空字符结尾）</td>
<td align="left">无</td>
</tr>
<tr>
<td align="center">输入整数</td>
<td align="center">5</td>
<td align="left">无</td>
<td align="left">读取的整数将存放在<code>a0</code></td>
</tr>
<tr>
<td align="center">输入字符串</td>
<td align="center">8</td>
<td align="left"><code>a0</code>存放字符串缓存的地址，<code>a1</code>存放最大允许输入的字符数</td>
<td align="left">无</td>
</tr>
<tr>
<td align="center">退出程序</td>
<td align="center">10</td>
<td align="left">无</td>
<td align="left">无</td>
</tr>
<tr>
<td align="center">退出程序</td>
<td align="center">93</td>
<td align="left"><code>a0</code>存放退出参数</td>
<td align="left">无</td>
</tr>
</tbody>
</table>
<p></center></p>
<div class="admonition example">
<p class="admonition-title">举个栗子 <img alt="🌰" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f330.svg" title=":chestnut:" /></p>
<p>&emsp;&emsp;若想打印寄存器<code>t0</code>，则可通过以下语句实现：</p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="w">    </span><span class="nf">ori</span><span class="w">   </span><span class="no">a0</span><span class="p">,</span><span class="w"> </span><span class="no">t0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span>
<span class="w">    </span><span class="nf">ori</span><span class="w">   </span><span class="no">a7</span><span class="p">,</span><span class="w"> </span><span class="no">zero</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span>
<span class="w">    </span><span class="nf">ecall</span>
</code></pre></div></td></tr></table></div>
</div>
<div class="admonition tip">
<p class="admonition-title">小提示 <img alt="💡" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4a1.svg" title=":bulb:" /></p>
<p>&emsp;&emsp;若有多个信息需要打印，可通过自定义宏来实现：</p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="na">.macro</span><span class="w"> </span><span class="no">print</span><span class="w"> </span><span class="nv">%reg</span><span class="p">,</span><span class="w"> </span><span class="nv">%mode</span>
<span class="w">    </span><span class="nf">ori</span><span class="w">   </span><span class="no">a0</span><span class="p">,</span><span class="w"> </span><span class="nv">%reg</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span>
<span class="w">    </span><span class="nf">ori</span><span class="w">   </span><span class="no">a7</span><span class="p">,</span><span class="w"> </span><span class="no">zero</span><span class="p">,</span><span class="w"> </span><span class="nv">%mode</span>
<span class="w">    </span><span class="nf">ecall</span>
<span class="na">.end_macro</span>

<span class="na">.text</span>
<span class="w">    </span><span class="na">......</span>
<span class="w">    </span><span class="nf">print</span><span class="w"> </span><span class="no">t0</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">      </span><span class="c1"># 打印t0寄存器的值</span>
<span class="w">    </span><span class="nf">print</span><span class="w"> </span><span class="no">t1</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="w">      </span><span class="c1"># 打印字符串（字符串地址存储在t1寄存器）</span>
<span class="w">    </span><span class="na">......</span>
</code></pre></div></td></tr></table></div>
</div>





                
              </article>
            </div>
          
          
  <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var tab,labels=set.querySelector(".tabbed-labels");for(tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script>

        </div>
        
      </main>
      
        <footer class="md-footer">
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-copyright">
  
    <div class="md-copyright__highlight">
      Copyright &copy; 2020 - 2024 哈尔滨工业大学（深圳）
    </div>
  
  
    Made with
    <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
      Material for MkDocs
    </a>
  
</div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    
    <script id="__config" type="application/json">{"base": "../..", "features": ["content.code.copy", "content.code.annotate", "content.tabs.link", "navigation.expand", "navigation.tabs"], "search": "../../assets/javascripts/workers/search.208ed371.min.js", "translations": {"clipboard.copied": "\u5df2\u590d\u5236", "clipboard.copy": "\u590d\u5236", "search.result.more.one": "\u5728\u8be5\u9875\u4e0a\u8fd8\u6709 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.more.other": "\u5728\u8be5\u9875\u4e0a\u8fd8\u6709 # \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.none": "\u6ca1\u6709\u627e\u5230\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.one": "\u627e\u5230 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.other": "# \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.placeholder": "\u952e\u5165\u4ee5\u5f00\u59cb\u641c\u7d22", "search.result.term.missing": "\u7f3a\u5c11", "select.version": "\u9009\u62e9\u5f53\u524d\u7248\u672c"}}</script>
    
    
      <script src="../../assets/javascripts/bundle.b4d07000.min.js"></script>
      
        <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML"></script>
      
    
  </body>
</html>